package com.example.test.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.console.Option;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.test.entity.AreaDemo;
import com.example.test.entity.BasicAdministrativeDivision;
import com.example.test.service.IAreaDemoService;
import com.example.test.service.IBasicAdministrativeDivisionService;
import io.reactivex.internal.operators.single.SingleHide;
import org.hibernate.engine.jdbc.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.nio.file.OpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <p>
 * 行政区域 前端控制器
 * </p>
 *
 * @author 牧羊人
 * @since 2022-03-24
 */
@RestController
@RequestMapping("/basic-administrative-division")
public class BasicAdministrativeDivisionController {


    @Autowired
    IAreaDemoService areaDemoService;

    @Autowired
    IBasicAdministrativeDivisionService basicAdministrativeDivisionService;



//    @PostConstruct
    public void ss111() {

        List<AreaDemo> list = areaDemoService.list();
        // sheng
        List<AreaDemo> shengList = list.stream().filter(areaDemo -> areaDemo.getCode().contains("0000")).collect(Collectors.toList());
        System.out.println("省: " + shengList.size());

        AtomicInteger i = new AtomicInteger();
        List<BasicAdministrativeDivision> list1 = shengList.stream().map(areaDemo -> {
            i.getAndIncrement();
            BasicAdministrativeDivision basicAdministrativeDivision = new BasicAdministrativeDivision();
            basicAdministrativeDivision.setCode(areaDemo.getCode());
            basicAdministrativeDivision.setName(areaDemo.getName());
            basicAdministrativeDivision.setLevel(1);
            basicAdministrativeDivision.setParentCode("0000");
            basicAdministrativeDivision.setSort(i.get());
            return basicAdministrativeDivision;
        }).collect(Collectors.toList());
        basicAdministrativeDivisionService.saveBatch(list1);
        System.out.println(list1.size() + "省");


        List<String> shengCodeList = shengList.stream().map(AreaDemo::getCode).collect(Collectors.toList());
        List<AreaDemo> shiList = list.stream().filter(areaDemo ->
                areaDemo.getCode().endsWith("00") && !shengCodeList.contains(areaDemo.getCode())
        ).collect(Collectors.toList());
        System.out.println("市:" + shiList.size());
        AtomicInteger num = new AtomicInteger(0);
        List<BasicAdministrativeDivision> shiDdemoList = shiList.stream().map(demo -> {
            String substring = demo.getCode().substring(0, 3);
            AreaDemo parentDemo = null;
            for (AreaDemo areaDemo : list) {
                if (areaDemo.getCode().equals(substring + "000")) {
                    parentDemo = areaDemo;
                }
            }
            if (null != parentDemo) {
                num.incrementAndGet();
                BasicAdministrativeDivision basicAdministrativeDivision = new BasicAdministrativeDivision();
                basicAdministrativeDivision.setCode(demo.getCode());
                basicAdministrativeDivision.setName(demo.getName());
                basicAdministrativeDivision.setLevel(2);
                basicAdministrativeDivision.setParentCode(parentDemo.getCode());
                basicAdministrativeDivision.setSort(num.get());
                return basicAdministrativeDivision;
            }
            return null;
        }).collect(Collectors.toList());
        for (BasicAdministrativeDivision basicAdministrativeDivision : shiDdemoList) {
            if (null!= basicAdministrativeDivision) {
                basicAdministrativeDivisionService.save(basicAdministrativeDivision);
            }
        }
        System.out.println(shiDdemoList.size());
        System.out.println(shiDdemoList.size());


        List<AreaDemo> quList = list.stream().filter(demo -> !demo.getCode().endsWith("00")).collect(Collectors.toList());
        AtomicInteger num1 = new AtomicInteger(0);
        List<BasicAdministrativeDivision> quDemoList = quList.stream().map(demo -> {
            String substring = demo.getCode().substring(0, 4);
            AreaDemo parentDemo = null;
            for (AreaDemo areaDemo : list) {
                if (areaDemo.getCode().equals(substring + "00")) {
                    parentDemo = areaDemo;
                }
            }
            if (null != parentDemo) {
                num1.incrementAndGet();
                BasicAdministrativeDivision basicAdministrativeDivision = new BasicAdministrativeDivision();
                basicAdministrativeDivision.setCode(demo.getCode());
                basicAdministrativeDivision.setName(demo.getName());
                basicAdministrativeDivision.setLevel(3);
                basicAdministrativeDivision.setParentCode(parentDemo.getCode());
                basicAdministrativeDivision.setSort(num1.get());
                return basicAdministrativeDivision;
            }
            return null;
        }).collect(Collectors.toList());
        for (BasicAdministrativeDivision basicAdministrativeDivision : quDemoList) {
            if (null!= basicAdministrativeDivision) {
                basicAdministrativeDivisionService.save(basicAdministrativeDivision);
            }
        }
        System.out.println("区:" + quList.size());
        System.out.println("区:" + quDemoList.size());


    }


    //    @PostConstruct
    public void ss() {
        List<AreaDemo> list = areaDemoService.list();

        // sheng
        List<AreaDemo> shengList = list.stream().filter(areaDemo -> areaDemo.getCode().contains("0000")).collect(Collectors.toList());
        AtomicInteger i = new AtomicInteger();
        List<BasicAdministrativeDivision> list1 = shengList.stream().map(areaDemo -> {
            i.getAndIncrement();
            BasicAdministrativeDivision basicAdministrativeDivision = new BasicAdministrativeDivision();
            basicAdministrativeDivision.setCode(areaDemo.getCode());
            basicAdministrativeDivision.setName(areaDemo.getName());
            basicAdministrativeDivision.setLevel(1);
            basicAdministrativeDivision.setParentCode("0000");
            basicAdministrativeDivision.setSort(i.get());
            return basicAdministrativeDivision;
        }).collect(Collectors.toList());
        basicAdministrativeDivisionService.saveBatch(list1);
        System.out.println(list1.size() + "省");

        // shi
        List<String> shiCodeList = shengList.stream().map(AreaDemo::getCode).collect(Collectors.toList());
        List<String> shengListCOde = shiCodeList.stream().map(code -> code.substring(0, 3)).collect(Collectors.toList());
        List<AreaDemo> shiAll = new ArrayList<>();
        for (int j = 0; j < shengListCOde.size(); j++) {
            List<AreaDemo> shiList = areaDemoService.list(Wrappers.lambdaQuery(AreaDemo.class).like(AreaDemo::getCode, shengListCOde.get(j)));
            int finalJ1 = j;
            List<AreaDemo> shis = shiList.stream().filter(areaDemo -> {
                if (areaDemo.getCode().endsWith("00") && !shiCodeList.contains(areaDemo.getCode())
                        && areaDemo.getCode().startsWith(shengListCOde.get(finalJ1))) {
                    return true;
                }
                return false;

            }).collect(Collectors.toList());
            shiAll.addAll(shis);
            int finalJ = j;
            AtomicInteger num = new AtomicInteger(0);
            List<BasicAdministrativeDivision> list2 = shis.stream().map(areaDemo -> {
                num.incrementAndGet();
                BasicAdministrativeDivision basicAdministrativeDivision = new BasicAdministrativeDivision();
                basicAdministrativeDivision.setCode(areaDemo.getCode());
                basicAdministrativeDivision.setName(areaDemo.getName());
                basicAdministrativeDivision.setLevel(2);
                basicAdministrativeDivision.setParentCode(shiCodeList.get(finalJ));
                basicAdministrativeDivision.setSort(num.get());
                return basicAdministrativeDivision;
            }).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(list2)) {
                basicAdministrativeDivisionService.saveBatch(list2, list2.size());
            }
        }
        System.out.println(shiAll.size() + "市");

        int count = 0;
        // qu
        List<String> shiCode = shiAll.stream().map(AreaDemo::getCode).collect(Collectors.toList());
        for (AreaDemo areaDemo : shiAll) {
            List<AreaDemo> quList = areaDemoService.list(Wrappers.lambdaQuery(AreaDemo.class).like(AreaDemo::getCode, areaDemo.getCode().substring(0, 4)));
            List<AreaDemo> quCollection = quList.stream().filter(areaDemo1 -> !shiCode.contains(areaDemo1.getCode())
                    && !shengListCOde.contains(areaDemo.getCode()) && areaDemo1.getCode().startsWith(areaDemo.getCode().substring(0, 4))).collect(Collectors.toList());
            count += quCollection.size();
            AtomicInteger qunum = new AtomicInteger(0);
            List<BasicAdministrativeDivision> list3 = quCollection.stream().map(demo -> {
                qunum.incrementAndGet();
                BasicAdministrativeDivision basicAdministrativeDivision = new BasicAdministrativeDivision();
                basicAdministrativeDivision.setCode(demo.getCode());
                basicAdministrativeDivision.setName(demo.getName());
                basicAdministrativeDivision.setLevel(3);
                basicAdministrativeDivision.setParentCode(areaDemo.getCode());
                basicAdministrativeDivision.setSort(qunum.get());
                return basicAdministrativeDivision;
            }).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(list3)) {
                basicAdministrativeDivisionService.saveBatch(list3, list3.size());
            }
        }

        System.out.println(count + "区");

    }
}
